import { Controller } from 'egg';

export default class SearchController extends Controller {

    private types = {
        1: '单曲', 10: '专辑', 100: '歌手', 1000: '歌单', 1002: '用户', 1004: 'MV', 1006: '歌词', 1009: '电台', 1014: '视频'
    }




    // /api/search
    public async search() {
        const { keywords, type, offset, limit } = this.ctx.query;
        const data = { type: this.types[type] && type || 1, limit: limit || 30, offset: offset || 0, s: keywords, csrf_token: '' };
        const url = `/weapi/search/get`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;

        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {

            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;

    }

    // /api/search/hot

    public async hot() {
        const data = { type: 1111 }
        const url = `/weapi/search/hot`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;
        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {
            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;
    }

    // search suggest
    public async suggest() {
        const { keywords, offset, limit, type } = this.ctx.query;
        const data = {
            csrf_token: "",
            s: keywords || "",
            offset: offset || 0,
            limit: limit || 30,
            type:this.types[type] && type ||1
        };
        const url = `/weapi/search/suggest/web`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;
        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {
            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;
    }

    // search/multimatch
    public async multimatch(){
        const {keywords,type}=this.ctx.query;
        const data = {
            csrf_token: "",
            s: keywords || "",
            type:this.types[type] && type ||1
        };
        const url = `/weapi/search/suggest/multimatch`;
        let result = await this.service.mequest.createWebAPIRequest(url, 'POST', data)
        const { body, cookie } = result;
        cookie && this.service.mequest.parseCookie(cookie).forEach(ck => {
            this.ctx.cookies.set(ck.cookie.name, ck.cookie.value, ck.options)
        })
        this.ctx.body = body;
    }

}